package org.stone926.airschool.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stone926.airschool.bean.Teacher;
import org.stone926.airschool.bean.info.AbstractInfo;
import org.stone926.airschool.constant.ErrorCode;
import org.stone926.airschool.dao.TeacherDao;

@Service
public class TeacherService {

	@Autowired
	private TeacherDao teacherDao;

	public Teacher getTeacherById(Integer tid) {
		return teacherDao.getTeacherById(tid);
	}

	public Teacher getTeacherByName(String tname) {
		return teacherDao.getTeacherByName(tname);
	}

	public boolean teacherLogin(String tname, String password) {
		Teacher s = getTeacherByName(tname);
		if (s == null)
			return false;

		if (s.getTeacherPassword().equals(password))
			return true;
		else
			return false;

	}

	public AbstractInfo teacherRegister(Teacher teacher) {
		if (teacher == null)
			return new AbstractInfo() {

				@Override
				public String getMessage() {
					return "用户名不能为空";
				}

				@Override
				public boolean isSuccess() {
					return false;
				}

				@Override
				public int getErrorCode() {
					return ErrorCode.TEACHER_NAME_IS_NULL;
				}
			};

		if (teacherDao.getTeacherByName(teacher.gettName()) != null)
			return new AbstractInfo() {

				@Override
				public boolean isSuccess() {
					return false;
				}

				@Override
				public String getMessage() {
					return "用户名重复，请更换";
				}

				@Override
				public int getErrorCode() {
					return ErrorCode.DUPLICATED_TEACHER_NAME;
				}

			};

		if (teacherDao.getTeacherByPhoneNumber(teacher.getTeacherPhonenumber()) != null)
			return new AbstractInfo() {

				@Override
				public String getMessage() {
					return "手机号已经被占用，请检查是否输入错误，并保护好个人信息安全";
				}

				@Override
				public boolean isSuccess() {
					return false;
				}

				@Override
				public int getErrorCode() {
					return ErrorCode.DUPLICATED_TEACHER_PHONENUMBER;
				}
			};

		teacherDao.insertTeacher(teacher);
		return new AbstractInfo() {

			@Override
			public String getMessage() {
				return "注册成功";
			}

			@Override
			public boolean isSuccess() {
				return true;
			}

			@Override
			public int getErrorCode() {
				return ErrorCode.REGISTER_SUCCESS;
			}
		};
	}
}
